/**
 * 实现数据处理
 * 创建者：Carburn Ashroom
 * 2025.2.10
 */

#include "data.h"

namespace Data {

    void Favorability::please(const QStringList& name)
    {
        for (const auto& n : name) {
            bool find {};
            for (auto& p : loves)
                if (p.first == n) {
                    ++p.second;
                    find = true;
                    break;
                }
            if (not find)
                loves.emplaceBack(n, 1);
        }
    }

    QStringList Favorability::select(const Favorability& judge) const
    {
        QStringList names;
        for (const auto& n : loves)
            for (const auto& jn : judge.loves)
                if (n.first==jn.first and n.second<=jn.second) {
                    names.append(n.first);
                    break;
                }
        return names;
    }

    Favorability Favorability::operator+(const Favorability& other) const
    {
        Favorability fav {*this};
        for (const auto& jn : other.loves)
            fav.please(jn);
        return fav;
    }

    void Favorability::operator+=(const Favorability& other)
    {
        for (const auto& jn : other.loves)
            please(jn);
    }

    void Favorability::init(QTextStream& message)
    {
        for ( ; ; ) {
            auto line = message.readLine();
            if (line.isEmpty())
                break;
            auto pair = line.split(' ');
            loves.emplaceBack(pair.front(), pair.back().toInt());
        }
    }

    void Favorability::please(const QPair<QString,int>& love)
    {
        bool find {};
        for (auto& p : loves)
            if (p.first == love.first) {
                p.second += love.second;
                find = true;
                break;
            }
        if (not find)
            loves.append(love);
    }

    QStringList Favorability::roles() const
    {
        QStringList res;
        for (const auto& l : loves)
            res.append(l.first);
        return res;
    }

    int Favorability::operator[](const QString& name) const
    {
        for (const auto& lv : loves)
            if (lv.first == name)
                return lv.second;
        return 0;
    }

    Road Road::operator+(const Road& other) const
    {
        Road res {*this};
        res.options.append(other.options);
        res.loves += other.loves;
        return res;
    }

    bool Roads::use_road(const QStringList& names, const Road& road)
    {
        if (names == loser) {
            roads.append(road);
            return true;
        }
        return false;
    }

    void Female_lead::add_road(const QStringList& loser, const Road& road)
    {
        for (auto& r : roads)
            if (r.use_road(loser, road))
                return;
        if (loser.isEmpty()) {
            roads.emplaceFront(loser);
            roads.front().use_road(loser, road);
        }
        else {
            roads.emplaceBack(loser);
            roads.back().use_road(loser, road);
        }
    }

}
